﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI;
using System.Web.UI.WebControls;

using JewererBasic.Classes;
using JewererBasic.Models;
using JewererBasic.BO;

namespace JewererBasic.Controllers
{
    public class ProductController : Controller
    {
        private ProductServiceImp productService;

        private CommonServiceImp commonService;

        public ProductController()
        {
            this.productService = new ProductServiceImp();
            this.commonService = new CommonServiceImp();
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetListProduct(string query)
        {
            List<PRODUCT> lst = this.productService.QueryProductId(query);
            List<Product> result = new List<Product>();
            foreach (var item in lst)
            {
                result.Add(Product.Convert(item));
            }
            return PartialView("_ListProduct", result);
        }

        public ActionResult Create()
        {
            Product product = new Product();
            this.setViewbag(product);
            return View(product);
        }

        [HttpPost]
        public ActionResult Create(Product product)
        {
            if (!ModelState.IsValid)
            {
                this.setViewbag(product);
                return View(product);
            }
            else
            {
                var file = product.MyFile;
                if (file.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(file.FileName);
                    var path = Path.Combine(Server.MapPath("~/Uploads"), fileName);
                    file.SaveAs(path);
                    product.Drawing = fileName;
                    this.productService.Create(Product.Convert(product));
                }
                return RedirectToAction("Index");
            }
        }

        public ActionResult Update(string id)
        {
            PRODUCT product = this.productService.FindProduct(id);
            if (product != null)
            {
                Product pro = Product.Convert(product);
                this.setViewbag(pro);
                return View(pro);
            }
            return HttpNotFound("Can't find Product has Id: " + id);
        }

        [HttpPost]
        public ActionResult Update(Product product)
        {
            if (!ModelState.IsValid)
            {
                this.setViewbag(product);
                return View(product);
            }
            else
            {                
                var file = product.MyFile;
                if (file != null && file.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(file.FileName);
                    var path = Path.Combine(Server.MapPath("~/Uploads"), fileName);
                    file.SaveAs(path);
                    product.Drawing = fileName;
                }
                bool result = this.productService.Update(Product.Convert(product));
                if (result == false)
                    return HttpNotFound("Can' update this product!");
                return RedirectToAction("Index");
            }
        }

        private void setViewbag(Product product)
        {
            ViewBag.IdKind = new SelectList(commonService.KINDs(), "Id", "Name", product.IdKind);
            ViewBag.IdType = new SelectList(commonService.TYPEs(), "Id", "Name", product.IdType);
            ViewBag.IdUnit = new SelectList(commonService.UNITs(), "Id", "Name", product.IdUnit);
            ViewBag.IdGamma = new SelectList(commonService.GAMMAs(), "Id", "Name", product.IdGamma);
        }

    }
}